Class yii\base\Object
Inheritance | yii\base\Object |
---|---|
Implements | yii\base\Configurable |
Subclasses | examples\shop\Product, examples\shop\ShopController, spinitron\dynamicAr\DynamicActiveQuery, spinitron\dynamicAr\DynamicActiveRecord, spinitron\dynamicAr\ValueExpression, tests\unit\data\BaseRecord, tests\unit\data\CustomerQuery, tests\unit\data\ar\Category, tests\unit\data\ar\Customer, tests\unit\data\ar\Item, tests\unit\data\ar\NullValues, tests\unit\data\ar\Order, tests\unit\data\ar\OrderItem, tests\unit\data\ar\OrderItemWithNullFK, tests\unit\data\ar\OrderWithNullFK, tests\unit\data\ar\Profile, tests\unit\data\ar\Type, tests\unit\data\dar\MissingDynColumn, tests\unit\data\dar\Person, tests\unit\data\dar\Product, tests\unit\data\dar\Supplier, yii\base\Action, yii\base\ActionEvent, yii\base\ActionFilter, yii\base\Behavior, yii\base\Component, yii\base\Controller, yii\base\DynamicModel, yii\base\ErrorHandler, yii\base\Event, yii\base\InlineAction, yii\base\Model, yii\base\ModelEvent, yii\base\Request, yii\base\Response, yii\base\Security, yii\base\Theme, yii\base\View, yii\base\ViewEvent, yii\base\ViewRenderer, yii\base\Widget, yii\db\ActiveQuery, yii\db\ActiveRecord, yii\db\AfterSaveEvent, yii\db\BaseActiveRecord, yii\db\BatchQueryResult, yii\db\ColumnSchema, yii\db\Command, yii\db\Connection, yii\db\DataReader, yii\db\Expression, yii\db\Migration, yii\db\Query, yii\db\QueryBuilder, yii\db\Schema, yii\db\TableSchema, yii\db\Transaction, yii\db\cubrid\QueryBuilder, yii\db\cubrid\Schema, yii\db\mssql\QueryBuilder, yii\db\mssql\Schema, yii\db\mssql\TableSchema, yii\db\mysql\QueryBuilder, yii\db\mysql\Schema, yii\db\oci\QueryBuilder, yii\db\oci\Schema, yii\db\pgsql\QueryBuilder, yii\db\pgsql\Schema, yii\db\sqlite\QueryBuilder, yii\db\sqlite\Schema |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/base/Object.php |
Object is the base class that implements the property feature.
A property is defined by a getter method (e.g. getLabel
), and/or a setter method (e.g. setLabel
). For example,
the following getter and setter methods define a property named label
:
private $_label;
public function getLabel()
{
return $this->_label;
}
public function setLabel($value)
{
$this->_label = $value;
}
Property names are case-insensitive.
A property can be accessed like a member variable of an object. Reading or writing a property will cause the invocation of the corresponding getter or setter method. For example,
// equivalent to $label = $object->getLabel();
$label = $object->label;
// equivalent to $object->setLabel('abc');
$object->label = 'abc';
If a property has only a getter method and has no setter method, it is considered as read-only. In this case, trying to modify the property value will cause an exception.
One can call hasProperty(), canGetProperty() and/or canSetProperty() to check the existence of a property.
Besides the property feature, Object also introduces an important object initialization life cycle. In particular, creating an new instance of Object or its derived class will involve the following life cycles sequentially:
- the class constructor is invoked;
- object properties are initialized according to the given configuration;
- the
init()
method is invoked.
In the above, both Step 2 and 3 occur at the end of the class constructor. It is recommended that
you perform object initialization in the init()
method because at that stage, the object configuration
is already applied.
In order to ensure the above life cycles, if a child class of Object needs to override the constructor, it should be done like the following:
public function __construct($param1, $param2, ..., $config = [])
{
...
parent::__construct($config);
}
That is, a $config
parameter (defaults to []
) should be declared as the last parameter
of the constructor, and the parent implementation should be called at the end of the constructor.
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Object |
__construct() | Constructor. | yii\base\Object |
__get() | Returns the value of an object property. | yii\base\Object |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Object |
__set() | Sets value of an object property. | yii\base\Object |
__unset() | Sets an object property to null. | yii\base\Object |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Object |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Object |
className() | Returns the fully qualified name of this class. | yii\base\Object |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Object |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\Object |
init() | Initializes the object. | yii\base\Object |
Method Details
Calls the named method which is not a class method.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
public mixed __call ( $name, $params ) | ||
$name | string | The method name |
$params | array | Method parameters |
return | mixed | The method return value |
---|---|---|
throws | \yii\base\UnknownMethodException | when calling unknown method |
Constructor.
The default implementation does two things:
- Initializes the object with the given configuration
$config
. - Call init().
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$config
here. - call the parent implementation at the end of the constructor.
public void __construct ( $config = [] ) | ||
$config | array | Name-value pairs that will be used to initialize the object properties |
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;
.
See also __set().
public mixed __get ( $name ) | ||
$name | string | The property name |
return | mixed | The property value |
---|---|---|
throws | \yii\base\UnknownPropertyException | if the property is not defined |
throws | \yii\base\InvalidCallException | if the property is write-only |
Checks if a property is set, i.e. defined and not null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property)
.
Note that if the property is not defined, false will be returned.
public boolean __isset ( $name ) | ||
$name | string | The property name or the event name |
return | boolean | Whether the named property is set (not null). |
---|
Sets value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $object->property = $value;
.
See also __get().
public void __set ( $name, $value ) | ||
$name | string | The property name or the event name |
$value | mixed | The property value |
throws | \yii\base\UnknownPropertyException | if the property is not defined |
---|---|---|
throws | \yii\base\InvalidCallException | if the property is read-only |
Sets an object property to null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($object->property)
.
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
public void __unset ( $name ) | ||
$name | string | The property name |
throws | \yii\base\InvalidCallException | if the property is read only. |
---|
Returns a value indicating whether a property can be read.
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also canSetProperty().
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string | The property name |
$checkVars | boolean | Whether to treat member variables as properties |
return | boolean | Whether the property can be read |
---|
Returns a value indicating whether a property can be set.
A property is writable if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string | The property name |
$checkVars | boolean | Whether to treat member variables as properties |
return | boolean | Whether the property can be written |
---|
Returns the fully qualified name of this class.
public static string className ( ) | ||
return | string | The fully qualified name of this class. |
---|
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists()
.
You may override this method when you implemented the php magic method __call()
.
public boolean hasMethod ( $name ) | ||
$name | string | The method name |
return | boolean | Whether the method is defined |
---|
Returns a value indicating whether a property is defined.
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true);
See also:
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string | The property name |
$checkVars | boolean | Whether to treat member variables as properties |
return | boolean | Whether the property is defined |
---|
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( ) |